import {message} from 'antd';


const post = (req, callback, fail) => {
    fetch(req.url, {
        method: 'POST',
        body: JSON.stringify(req.body),
        headers: {
            'content-type': 'application/json'
        }
    }).then(res => {
        res.json().then((data) => {

            console.log('请求：', req, '结果：', data);

            if (!data || data.success !== true) {
                let msg = '请求失败';
                if (data && data.errorMsg) {
                    msg = data.errorMsg;
                }
                message.error(msg);

                if (fail){
                    fail(data);
                }
                return;
            }

            callback(data);
        })
    }).catch(e => {
        message.error('请求失败');
        if (fail){
            fail();
        }
    });
};


const getRequest = (param, url) => {
    let req = {};
    req.url = url;
    req.body = param;
    return req;
};


export default {
    post: post,
    getRequest: getRequest
}
